<?php
namespace app\api\controller;
use think\Controller;
class Index extends Base
{
    public function index()
    {   
        return $this->fetch();
    }  

    public function vegetables()
    {  
       
       $data = [
            'zhuangtai' =>1
         ];
           $info = model('Vegetables')
                      ->alias("v")
                      ->join('gongyingshang g','g.gid=v.gid','LEFT')
                      ->field('v.vid,v.url,v.pzleixing,v.vname,v.price,g.gname')
                      ->where($data)
                      ->select();
          $info['success'] = true;
             ajaxmsg('success','ok',1,$info);
    }
    


      //获得根据销售类型分类的蔬菜（第一个数组，数组各项分别为热销，新品，打折类型）和根据品种类型分类的蔬菜(第二个数组，数组各项分别为蔬菜水果，禽食蛋肉，熟食豆，米面粮油类型)
      public function getVegetables()
      {
         $info = model('Vegetables')->getVegetables();
      //   dump($info);exit();
           if ($info) {
              // ajaxmsg('true','ok',200,$info);
              $json['success'] = 'true';
              $json['msg'] = 'ok';
              $json['status'] = 200;
              $json['data'] = $info;
              $m = json_encode($json,JSON_UNESCAPED_UNICODE);
              echo $m;
            }else{
                $json['success'] = 'false';
                $json['msg'] = '请求数据失败';
                $json['status'] =400;
                $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                echo $m;
            }
      }
    


     //获取短信验证码
      public function getPhoneCode($phone)
      {
         $phone = $_GET['phone'];
         $code = mt_rand(111111,999999); 
         //(注册用户账号同时把phone写入verifycode表)，验证手机号是否在verifycode存在，存在的话，该手机号已经注册，更新该手机号码的code,发送随机验证码到控制台，不存在则提示未注册
          $res= model('Verifycode')->where("phone='{$phone}'")->find();
           if ($res) {
                  $count = model('Verifycode')->where("id='{$res->id}'")->update(array('code'=>$code));
                  if($count){
                     $code = model('Verifycode')->where("id='{$res->id}'")->value('code');
                  }
              // ajaxmsg('true',"验证码获取成功",200, $code);
                  $json['success'] = 'true';
                  $json['msg'] = "验证码获取成功";
                  $json['status'] = 200;
                  $json['code'] = $code;
                $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                echo $m;
           }else{
               // ajaxmsg('false',"该手机号尚未注册",500);
                 $json['success'] = 'false';
                 $json['msg'] = '改手机号尚未注册';
                 $json['status'] = 500;
                $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                echo $m;
           }            
       }


       //手机验证码登录,从verifycode表中根据前端页面传过来的手机号查询对应的验证码code，跟前端页面传过来的code比对，若相同，则登录成功，不同则提示验证码错误
       public function phoneCodeLogin($phone,$code)
       {
            $phone = $_GET['phone'];
            $code = $_GET['code'];
            $res = model('Verifycode')->where("phone='{$phone}'")->value('code');
            //获得用户id,token,用户名，手机号,用户类型
            $info = model('User')->getUserInfo($phone); 
            if ($res === $code) {
                // ajaxmsg('true',"登录成功", 200, $res);
                  $json['success'] = 'true';
                  $json['msg'] = '登录成功';
                  $json['status']=200;
                $json['data'] = $info;
                $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                echo $m;
            }else{
               // ajaxmsg('false',"验证码错误",500,$code);
                  $json['success'] = 'false';
                  $json['msg'] ='验证码不正确';
                  $json['status'] = 400;
                $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                echo $m;
            }
       }

      
      //添加上商品到购物车
      public function addGoodsToCart($token,$vid,$vname,$price,$pzleixing)
       {          
           $token = $_GET['token'];
           $vname=$_GET['vname'];
           $price=$_GET['price'];
           $vid = $_GET['vid'];
                //    return $vname;
          //查找zhuangtai=-1,num=0,goods_id=$vid,token=$token的数据
          
          //根据$vid,$token查询商品
          $res = model('Shopcart')->getGoods($token,$vid);
          //根据蔬菜商品id,用户token查询shopcart表是否存在该商品，若不存在，则创建该商品，num=1,若存在，则num+1
          if ($res) {
             //  $count = model('shopCart')->where('id',$res->id)->setInc('num');
               // if ($count) {
               //    //返回值不为空，
               //     $json['success']='true';
               //     $json['msg'] = '添加购物车成功';
               //     $json['status']=200;
               //     //$m = $res->name;
               //   $m = json_encode($json,JSON_UNESCAPED_UNICODE);
               //  echo $m;
               // }
              // return $res->zhuangtai;
               //如果表中存在商品。(1)zhuangtai=-1,更新zhuangtai的值为1，(zhuangtai的值为-1，num原值为0)num+1;(2)zhuangtai=1,num直接+1
                   if ($res->zhuangtai === -1) {
                        $count = model('shopcart')->where('id',$res->id)->setField('zhuangtai',1);
                        $checked = model('shopcart')->where('id',$res->id)->setField('checked',1);
                   }
                     $res = model('shopCart')->where('id',$res->id)->setInc('num');
                   if ($res) {
                      //返回值不为空，
                       $json['success']='true';
                       $json['msg'] = '添加购物车成功';
                       $json['status']=200;
                       //$m = $res->name;
                     $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                    echo $m;
               }
          }else{
               //创建商品，num=1
               $data = ['name'=>$vname,'token'=>$token,'goods_id'=>$vid,'img'=>'1','price'=>$price,'num'=>1,'zhuangtai'=>1,'checked'=>1,'pzleixing'=>$pzleixing];
                $count = model('shopCart')->insert($data);
                if ($count) {
                  //返回值不为空，
                   $json['success']='true';
                   $json['msg'] = '添加购物车成功';
                   $json['status']=200;
                 //   $m = $res->name;
                 $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                 echo $m;
                }
          }

      }

     
    //获取当前用户(token为$token)购物车中的商品
     public function getGoodsCart($token)
     { 
    //   $token = $_GET['token'];
        $res=model('shopcart')->getGoodsCart($token);
      //   dump($res[0]->toarray());exit();
        if ($res) {
             $json['success']='true';
             $json['msg']='初始化购物车数据成功';
             $json['status']=200;
             $json['data'] = $res;
              $m = json_encode($json,JSON_UNESCAPED_UNICODE);
              echo $m;
        }else{
             $json['success']='false';
             $json['msg']='当前用户暂无购物车数据';
             $json['status']=500;
             $m = json_encode($json,JSON_UNESCAPED_UNICODE);
              echo $m;
        }


     }


     public function removeOutCart($token,$goodsId)
     {   


         //根据goodsId，token去shopcart表查找商品，商品存在,goodsNum>1的情况下，移除，num-1；goodsNum=1情况下,移除，zhuangtai=-1,num-1
             $data = [
                 'token'=>$token,
                 'goods_id'=>$goodsId
             ];
             $res = model('shopcart')->where($data)->find();
            // return $res->num;
                 if($res->num === 1 ){
                    $count = model('shopcart')->where('id',$res->id)->setField('zhuangtai',-1);
                  }
               $r =  model('shopcart')->where('id',$res->id)->setDec('num');
               if ($r) {
                     return "移除购物车成功";
               }else{
                    return "失败";
               }
         
     }

      //清空购物车
      public function clearAllCart($token)
      {   
          $count = model('shopcart')->where('token',$token)->update(['num'=>0,'zhuangtai'=>-1]);
          if ($count) {
                $json['success']='true';
                $json['msg']='成功清空购物车数据';
                $json['status']=200;
                $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                echo $m;
            }else{
               echo "该用户购物车暂无商品";
            }  
      }


      //根据手机号码去用户表查询该手机号码是否已经被注册
      public function verifyPhone($phone)
      {
         $res = model('user')->where('mobile',$phone)->find();
         if ($res) {
            $json['success']='false';
            $json['msg']='该号码已被注册';
            $json['status']=500;
            $m = json_encode($json,JSON_UNESCAPED_UNICODE);
            echo $m;
         }else{
            $json['success']='true';
            $json['msg']='该号码可以注册';
            $json['status']=200;
            $m = json_encode($json,JSON_UNESCAPED_UNICODE);
            echo $m;
         }
      }
      //根据账号去用户表查询该账号是否已经被注册
      public function verifyZhangHao($zhanghao)
      {
         $res = model('user')->where('zhanghao',$zhanghao)->find();
         if ($res) {
            $json['success']='false';
            $json['msg']='该账号已被注册';
            $json['status']=500;
            $m = json_encode($json,JSON_UNESCAPED_UNICODE);
            echo $m;
         }else{
            $json['success']='true';
            $json['msg']='该账号可以注册';
            $json['status']=200;
            $m = json_encode($json,JSON_UNESCAPED_UNICODE);
            echo $m;
         }
      }

      //注册用户
      public function regist($username,$zhanghao,$password,$phone)
      {    
           $count = model('user')->insert($username,$zhanghao,$password,$phone);
           if ($count) {
               $json['success']='true';
               $json['msg']='注册成功';
               $json['status']='200';
             $m = json_encode($json,JSON_UNESCAPED_UNICODE);
             echo $m;          
           }else{
               $json['success']='false';
               $json['msg']='注册失败';
               $json['status']='500';
             $m = json_encode($json,JSON_UNESCAPED_UNICODE);
             echo $m;     
           }
      }
   
       //查找zhanghao为$zhanghao的用户的uid,将该uid插入到xhmimagui
      public function getxhgui($zhanghao)
      {
          $res = model('user')->where('zhanghao',$zhanghao)->find();
           $id = model('xhmimagui')->insert($res->uid);
           if ($id) {
               $json['success']='true';
               $json['msg']='鲜活密码柜创建成功';
               $json['zhanghao']=$zhanghao;
               $json['bianhao']=$id;
               $json['status']='200';
             $m = json_encode($json,JSON_UNESCAPED_UNICODE);
             echo $m;    
           }else{
               $json['success']='false';
               $json['msg']='鲜活密码柜创建失败';
               $json['status']='500';
             $m = json_encode($json,JSON_UNESCAPED_UNICODE);
             echo $m; 
           }
      }


     public function getybgui($zhanghao)
     {
          $res = model('user')->where('zhanghao',$zhanghao)->find();
           $id = model('ybmimagui')->insert($res->uid);
           if ($id) {
               $json['success']='true';
               $json['msg']='一般蔬菜商品密码柜创建成功';
               $json['zhanghao']=$zhanghao;
               $json['bianhao']=$id;
               $json['status']='200';
             $m = json_encode($json,JSON_UNESCAPED_UNICODE);
             echo $m;    
           }else{
               $json['success']='false';
               $json['msg']='一般蔬菜商品创建失败';
               $json['status']='500';
             $m = json_encode($json,JSON_UNESCAPED_UNICODE);
             echo $m; 
           }       
     }
      //保存$phone到verifycode表中
     public function savePhone($phone)
     {
        $id = model('verifycode')->insertPhone($phone);
         if ($id) {
               $json['success']='true';
               $json['msg']='成功添加手机验证码登录功能';
               $json['status']='200';
             $m = json_encode($json,JSON_UNESCAPED_UNICODE);
             echo $m;    
           }else{
               $json['success']='false';
               $json['msg']='添加手机验证码登录功能失败';
               $json['status']='500';
             $m = json_encode($json,JSON_UNESCAPED_UNICODE);
             echo $m; 
           }       
     }

     //手机验证码登录，判断用户账号是否被禁用
      public function memberStatus($phone)
      {  
       // echo $phone;
         $zhuangtai = model('user')->where('mobile',$phone)->value('zhuangtai');
          if ($zhuangtai) {
               $json['success']='true';
               $json['msg']='获取用户状态成功';
               $json['status']='200';
               $json['data'] = $zhuangtai;
             $m = json_encode($json,JSON_UNESCAPED_UNICODE);
             echo $m;    
           }else{
               $json['success']='false';
               $json['msg']='获取用户状态失败';
               $json['status']='500';
             $m = json_encode($json,JSON_UNESCAPED_UNICODE);
             echo $m; 
           } 
      }
      //创建已经支付的订单
    public function createyzfOrder($token,$beizhu,$totalPrice,$servicetime)
    {    
        //oid为订单id
         $oid = model('ordermaster')->insertyzfOrder($token,$beizhu,$totalPrice,$servicetime);
         $result = model('ordermaster')->where('id',$oid)->find();
         if ($result) {
             $json['success']='true';
             $json['msg']='订单表创建成功，状态为已支付';
             $json['status']='200';
             $json['bianhao']=$result->bianhao;
             $m = json_encode($json,JSON_UNESCAPED_UNICODE);
             echo $m; 
         }else{
             $json['success']='false';
             $json['msg']='订单表创建失败';
             $json['status']='500';
             $m = json_encode($json,JSON_UNESCAPED_UNICODE);
             echo $m; 
         }
    }
      //创建预支付的订单
      public function createwzfOrder($token,$beizhu,$totalPrice,$servicetime)
    {  
         $oid = model('ordermaster')->insertwzfOrder($token,$beizhu,$totalPrice,$servicetime);
         $result = model('ordermaster')->where('id',$oid)->find();
         if ($oid) {
             $json['success']='true';
             $json['msg']='订单表创建成功，状态为未支付';
             $json['status']='200';
             $json['bianhao']=$result->bianhao;
             $m = json_encode($json,JSON_UNESCAPED_UNICODE);
             echo $m; 
         }else{
             $json['success']='false';
             $json['msg']='订单表创建失败';
             $json['status']='500';
             $m = json_encode($json,JSON_UNESCAPED_UNICODE);
             echo $m; 
         }
    }
     //创建订单编号为$bianhao的订单详情表
    public function createdOrderDetail($bianhao)
    {
        //从购物车表获取zhuangtai=1，checked=1的商品，存入商品详情表(bianhao字段值为$bianhao)，将购物车表中checked值为1的商品的zhuangtai设置为-1，num设置为0(将商品移出购物车)
        //获取购物车中已选中的商品
          $goods = model('shopcart')->selectedGoods();
        //   echo count($goods);
              $count = 0;
              for($i=0;$i<count($goods);$i++){
                  $data=['bianhao'=>$bianhao,'name'=>$goods[$i]->name,'price'=>$goods[$i]->price,'img'=>$goods[$i]->img,'num'=>$goods[$i]->num,'zhuangtai'=>1,'vid'=>$goods[$i]->goods_id];
                  model('orderdetail')->insert($data);
                // //将选中的商品移出购物车
                  model('shopcart')->where('id',$goods[$i]->id)->update(['num' => 0,'zhuangtai'=>-1]);
                  $count++;
              }
              if ($count) {
                 $json['success']='true';
                 $json['msg']='订单详情表创建完成';
                 $json['status']='200';
                 $json['count']=$count;
                 $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                 echo $m; 
             }else{
                 $json['success']='false';
                 $json['msg']='订单详情表创建失败';
                 $json['status']='500';
                 $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                 echo $m; 
             }
    }
     //获取支付时间
     public function getPayTime($bianhao)
     {
         $payTime = model('ordermaster')->where('bianhao',$bianhao)->value('create_time');
            if ($payTime) {
                 $json['success']='true';
                 $json['msg']='支付时间获取成功';
                 $json['status']='200';
                 $json['payTime']=$payTime;
                 $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                 echo $m; 
             }else{
                 $json['success']='false';
                 $json['msg']='支付时间获取失败';
                 $json['status']='500';
                 $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                 echo $m; 
             }
     }

   //购物车商品的选中和取消
    public function singerGoodsSelected($token,$goodsid)
    {    
          $data = [
            'token' => $token,
            'goods_id'=>$goodsid
          ];
         $res = model('shopcart')->where($data)->find();
         if ($res->checked===1) {
              $count = model('shopcart')->where($data)->setField('checked',0);
           //   dump("选中");exit(); 
         }else if($res->checked===0){
            $count = model('shopcart')->where($data)->setField('checked',1);
            //dump("取消");exit();
         }
        if ($count) {
             $json['success']='true';
             $json['msg']='取消或选中成功';
             $json['status']='200';
             $json['checked']=$res->checked;
             $m = json_encode($json,JSON_UNESCAPED_UNICODE);
             echo $m; 
        }else{
             $json['success']='false';
             $json['msg']='取消或者选中失败';
             $json['status']='500';
             $m = json_encode($json,JSON_UNESCAPED_UNICODE);
             echo $m; 
        }
    }
    
    //全选
    public function selectedAll($token)
    { 
      $result = model('shopcart')->selectedAll($token);
        if ($result) {
             $json['success']='true';
             $json['msg']='全选成功';
             $json['status']='200';
             $json['count']=$result;
             $m = json_encode($json,JSON_UNESCAPED_UNICODE);
             echo $m; 
        }else{
             $json['success']='false';
             $json['msg']='全选失败';
             $json['status']='500';
             $m = json_encode($json,JSON_UNESCAPED_UNICODE);
             echo $m; 
        }
    }


    //取消全选
    public function selectedNo($token)
    {
       $result = model('shopcart')->selectedNo($token);
        if ($result) {
             $json['success']='true';
             $json['msg']='取消全选成功';
             $json['status']='200';
             $json['count']=$result;
             $m = json_encode($json,JSON_UNESCAPED_UNICODE);
             echo $m; 
        }else{
             $json['success']='false';
             $json['msg']='取消全选失败';
             $json['status']='500';
             $m = json_encode($json,JSON_UNESCAPED_UNICODE);
             echo $m; 
        }

    }


    public function getOrders($token)
    {
        //echo $token;
         $data = [
              'token'=>$token,
               'zhuangtai'=>1,
          ];
          $orders = model('ordermaster')->getOrders($token);
          $time = model('ordermaster')->where('token',$token)->column('create_time');
      
         for ($i=0; $i < count($orders); $i++) { 
                  $orders[$i]['time']=$time[$i];           
          }  
          if ($orders) {
               $json['success']='true';
               $json['msg']='订单数据获取成功';
               $json['status']='200';
                $json['data']=$orders;
               $m = json_encode($json,JSON_UNESCAPED_UNICODE);
               echo $m; 
          }else{
               $json['success']='false';
               $json['msg']='订单数据获取失败';
               $json['status']='500';
               $m = json_encode($json,JSON_UNESCAPED_UNICODE);
               echo $m; 
          }
    }


    public function deleteOrder($id)
    {
        $count = model('ordermaster')->where('id',$id)->setField('zhuangtai',-1);
          if ($count) {
               $json['success']='true';
               $json['msg']='订单删除成功';
               $json['status']='200';
               $m = json_encode($json,JSON_UNESCAPED_UNICODE);
               echo $m; 
          }else{
               $json['success']='false';
               $json['msg']='订单数据删除失败';
               $json['status']='500';
               $m = json_encode($json,JSON_UNESCAPED_UNICODE);
               echo $m; 
          }    
    }

     //付款提交
    public function payOrder($id)
    {
        $count = model('ordermaster')->where('id',$id)->setField('status', 1);
        if ($count) {
               $json['success']='true';
               $json['msg']='付款成功';
               $json['status']='200';
               $m = json_encode($json,JSON_UNESCAPED_UNICODE);
               echo $m; 
          }else{
               $json['success']='false';
               $json['msg']='付款失败';
               $json['status']='500';
               $m = json_encode($json,JSON_UNESCAPED_UNICODE);
               echo $m; 
          }  

    }

    //生成积分记录
    public function createJF($token,$bianhao,$add,$reduce){
          if ($reduce!=0) {
            //$reduct!=0,用户有积分，jifen表生成两条记录(购买蔬菜获得积分，抵扣记录)
              $count1 = model('jifen')->add($token,$bianhao,$add);
              $count2 = model('jifen')->reduce($token,$bianhao,$reduce);
               if ($count1&&$count2) {
                   $json['success']='true';
                   $json['msg']='成功插入两条积分记录';
                   $json['status']='200';
                   $json['add']=$add;
                   $json['reduce']=$reduce;
                   $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                   echo $m; 
               }
              else{
                   $json['success']='false';
                   $json['msg']='插入两条记录失败';
                   $json['status']='500';
                   $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                   echo $m; 
              };
          }else{
            //用户没有积分，jifen表生成一条记录(购买蔬菜获得积分)
              $count = model('jifen')->add($token,$bianhao,$add);
               if ($count) {
                   $json['success']='true';
                   $json['msg']='成功插入一条条积分记录';
                   $json['status']='200';
                   $json['add']=$add;
                   $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                   echo $m; 
               }else{
                   $json['success']='false';
                   $json['msg']='插入一条积分记录失败';
                   $json['status']='500';
                   $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                   echo $m; 
               }
          }
    }
     
    public function updateJF($token,$add,$reduce){
          $jifen = model('user')->updateJF($token,$add,$reduce);
                if ($jifen) {
                   $json['success']='true';
                   $json['msg']='更新用户积分成功';
                   $json['status']='200';
                   $json['data']=$jifen;
                   $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                   echo $m; 
               }else{
                   $json['success']='false';
                   $json['msg']='更新用户积分失败';
                   $json['status']='500';
                   $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                   echo $m; 
               }         
    }
   
    public function getSearch($vname){
       $result = model('vegetables')->where('vname','like',"%$vname%")->select();
                if ($result) {
                   $json['success']='true';
                   $json['msg']='搜索数据成功';
                   $json['status']='200';
                   $json['data']=$result;
                   $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                   echo $m; 
               }else{
                   $json['success']='false';
                   $json['msg']='搜索数据失败';
                   $json['status']='500';
                   $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                   echo $m; 
               }   
    }

    public function getUser($token){
         $result = model('user')->getUser($token);
          if ($result) {
                   $json['success']='true';
                   $json['msg']='用户信息获取成功';
                   $json['status']='200';
                   $json['data']=$result;
                   $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                   echo $m; 
               }else{
                   $json['success']='false';
                   $json['msg']='用户信息获取失败';
                   $json['status']='500';
                   $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                   echo $m; 
               }   
    }

    public function getUserByName($name){
       $res = model('user')->where('username',$name)->find();
       //dump($res);exit();
        if ($res) {
                   $json['success']='true';
                   $json['msg']='该用户名已存在';
                   $json['status']='200';
                   $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                   echo $m; 
               }else{
                   $json['success']='false';
                   $json['msg']='该用户名不存在';
                   $json['status']='500';
                   $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                   echo $m; 
               }   
    }

    public function UpdateUserName($token,$name){
         $count =model('user')->updateName($token,$name);
        if ($count) {
                   $json['success']='true';
                   $json['msg']='用户名更新成功';
                   $json['status']='200';
                   $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                   echo $m; 
               }else{
                   $json['success']='false';
                   $json['msg']='用户名更新失败';
                   $json['status']='500';
                   $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                   echo $m; 
               } 
    }


    public function getUserByZH($zhanghao){
       $res = model('user')->where('zhanghao',$zhanghao)->find();
               if ($res) {
                   $json['success']='true';
                   $json['msg']='该账号已存在';
                   $json['status']='200';
                   $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                   echo $m; 
               }else{
                   $json['success']='false';
                   $json['msg']='该账号不存在';
                   $json['status']='500';
                   $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                   echo $m; 
               } 
    }

    public function UpdateUserZH($token,$zhanghao){
         $count = model('user')->updateZH($token,$zhanghao);
         if ($count) {
                   $json['success']='true';
                   $json['msg']='账号更新成功';
                   $json['status']='200';
                   $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                   echo $m; 
               }else{
                   $json['success']='false';
                   $json['msg']='账号更新失败';
                   $json['status']='500';
                   $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                   echo $m; 
               } 
    }

    public function getUserByPasswd($token,$oldpasswd)
    {
       $res = model('user')->where('token',$token)->find(); 
        if ($res->password===$oldpasswd) {
           $json['success']='true';
               $json['msg']='旧密码正确';
               $json['status']='200';
               $m = json_encode($json,JSON_UNESCAPED_UNICODE);
               echo $m; 
        }else{
               $json['success']='false';
               $json['msg']='旧密码错误';
               $json['status']='500';
               $m = json_encode($json,JSON_UNESCAPED_UNICODE);
               echo $m;    
        }
    }

    public function UpdatePasswd($token,$newpasswd)
    {
         $count = model('user')->where('token',$token)->update(['password'=>$newpasswd]);
         if ($count) {
                   $json['success']='true';
                   $json['msg']='密码更新成功';
                   $json['status']='200';
                   $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                   echo $m; 
               }else{
                   $json['success']='false';
                   $json['msg']='密码更新失败';
                   $json['status']='500';
                   $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                   echo $m; 
               } 
     }

     public function getUserByPhone($phone)
     {
       $res = model('user')->where('mobile',$phone)->find();
               if ($res) {
                   $json['success']='true';
                   $json['msg']='该手机已存在';
                   $json['status']='200';
                   $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                   echo $m; 
               }else{
                   $json['success']='false';
                   $json['msg']='该手机不存在';
                   $json['status']='500';
                   $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                   echo $m; 
               }  
     }

     public function UpdateUserPhone($token,$mobile){
         $count = model('user')->where('token',$token)->update(['mobile'=>$mobile]);
         if ($count) {
                   $json['success']='true';
                   $json['msg']='手机更新成功';
                   $json['status']='200';
                   $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                   echo $m; 
               }else{
                   $json['success']='false';
                   $json['msg']='手机更新失败';
                   $json['status']='500';
                   $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                   echo $m; 
               } 
     }

     public function getYB($uid){
          $bianhao = model('ybmimagui')->where('uid',$uid)->value('bianhao');
         //  dump("da");exit();
         $mima = model('ybmimagui')->where('uid',$uid)->value('mima');
          if ($bianhao&&$mima) {
                   $json['success']='true';
                   $json['msg']='获取柜号密码成功';
                   $json['status']='200';
                   $json['bianhao']=$bianhao;
                   $json['mima']=$mima;
                   $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                   echo $m; 
               }else{
                   $json['success']='false';
                   $json['msg']='手机更新失败';
                   $json['status']='500';
                   $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                   echo $m; 
               }   
     }
     public function getXH($uid){
          $bianhao = model('xhmimagui')->where('uid',$uid)->value('bianhao');
         //  dump("da");exit();
         $mima = model('xhmimagui')->where('uid',$uid)->value('mima');
          if ($bianhao&&$mima) {
                   $json['success']='true';
                   $json['msg']='获取柜号密码成功';
                   $json['status']='200';
                   $json['bianhao']=$bianhao;
                   $json['mima']=$mima;
                   $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                   echo $m; 
               }else{
                   $json['success']='false';
                   $json['msg']='手机更新失败';
                   $json['status']='500';
                   $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                   echo $m; 
               }   
     }

     public function tobeCGY($uid){
           $count = model('user')->where('uid',$uid)->setField('userleixing', 1);
              if ($count) {
                   $json['success']='true';
                   $json['msg']='采购员申请成功';
                   $json['status']='200';
                   $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                   echo $m; 
               }else{
                   $json['success']='false';
                   $json['msg']='采购员申请失败';
                   $json['status']='500';
                   $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                   echo $m; 
               } 
     }

    //前台获取采购信息
     public function getCaigou()
     {
     // echo "string";
          $data = [
             'zhuangtai'=>1,
             'status'=>-1,
          ];
           $res = model('caigou')
                   ->field('id,token,bianhao,jifen,status')
                   ->where($data)
                   ->select();
          $time = model('caigou')->where($data)->column('create_time');  
         for ($i=0; $i < count($res); $i++) { 
                  $res[$i]['time']=$time[$i];           
          };  
         for ($i=0; $i <count($res);$i++) { 
               $res[$i]['xuhao']=$i+1;
         };
    // dump($res);exit();
        if ($res) {
            $json['success']='true';
            $json['msg']='采购数据获取成功';
            $json['status']=200;
            $json['data']=$res;
            $m = json_encode($json,JSON_UNESCAPED_UNICODE);
            echo $m;
         }else{
            $json['success']='false';
            $json['msg']='采购信息获取失败';
            $json['status']=500;
            $m = json_encode($json,JSON_UNESCAPED_UNICODE);
            echo $m;
         }   
      }

      //前台获取采购信息详情
      public function getCaigouDetail($bianhao,$token)
      { 
           $data = [
               'bianhao'=>$bianhao,
               'token'=>$token,
           ];
           $servicetime = model('ordermaster')->field('servicetime')->where($data)->find();
           $jifen = model('caigou')->field('jifen')->where($data)->find();
           $res = model('user')->field('mobile,username')->where('token',$token)->find();
         
            $caigou = model('vegetables')
                 ->alias('v')
                 ->join('orderdetail o','v.vid=o.vid')
                 ->join('gongyingshang g','v.gid=g.gid')
                 ->field('g.gname,v.vname,o.num,g.phone,g.email,g.dizhi')
                 ->where(['o.bianhao'=>$bianhao])
                 ->select();
             $uid = model('user')->where('token',$token)->value('uid');
             $ybmimagui = model('ybmimagui')->where('uid',$uid)->value('bianhao');
             $xhmimagui = model('xhmimagui')->where('uid',$uid)->value('bianhao');
            for ($i=0; $i <count($caigou) ; $i++) { 
               $caigou[$i]['xuhao'] = $i+1;
            };


          if ($servicetime&&$jifen&&$res&&$caigou) {
                $json['success']='true';
                $json['msg']='采购详情数据获取成功';
                $json['status']=200;
                $json['servicetime']=$servicetime;
                $json['jifen']=$jifen;
                $json['res']=$res;
                $json['data']=$caigou;
                $json['ybmimagui'] = $ybmimagui;
                $json['xhmimagui'] = $xhmimagui;
                $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                echo $m;
             }else{
                $json['success']='false';
                $json['msg']='采购详情数据获取失败';
                $json['status']=500;
                $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                echo $m;
             }   

      } 


     //申请采购
     public function shenqingCG($bianhao,$caigouyuan)
     {  
        //插入采购员
        $count = model('caigou')->where('bianhao',$bianhao)->setField('caigouyuan',$caigouyuan);
         //改变采购信息状态为审核中
         $count2 = model('caigou')->where('bianhao',$bianhao)->setField('status',0);
         
            if ($count&&$count2) {
                $json['success']='true';
                $json['msg']='申请成功';
                $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                echo $m;
             }else{
                $json['success']='false';
                $json['msg']='申请失败';
                $json['status']=500;
                $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                echo $m;
             }   

     }

     public function getMyCaigou($token)
     {  
         $res = model('caigou')->field('bianhao,jifen,status,token')->where('caigouyuan',$token)->select();
          $time = model('caigou')->column('create_time');
       //  dump($time);exit();
           for ($i=0; $i < count($res); $i++) { 
             $res[$i]['time']=$time[$i];           
          }; 
          for ($i=0; $i < count($res); $i++) { 
             $res[$i]['xuhao']=$i+1;
          };
         //dump($res);exit();
            if ($res) {
                $json['success']='true';
                $json['msg']='获取我的采购信息成功';
                $json['status']= '200';
                $json['data']=$res;
                $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                echo $m;
             }else{
                $json['success']='false';
                $json['msg']='获取我的采购细信息失败';
                $json['status']=500;
                $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                echo $m;
             }   
     }

     public function getDetail($bianhao)
     {

       $res = model('orderdetail')->field('name,num,price')->where('bianhao',$bianhao)->select();
       //dump($res);exit();
       for ($i=0; $i <count($res) ; $i++) { 
          $res[$i]['xuhao'] = $i+1;
       }
            if ($res) {
                $json['success']='true';
                $json['msg']='获取我的采购信息成功';
                $json['status']= '200';
                $json['data']=$res;
                $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                echo $m;
             }else{
                $json['success']='false';
                $json['msg']='获取我的采购细信息失败';
                $json['status']=500;
                $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                echo $m;
             }  
     }
      //确认收货
      public function queren($bianhao)
      {  
        // echo $bianhao;
        // 改变订单表订单状态
          $res = model('ordermaster')->where('bianhao',$bianhao)->setField('status', 2);
       //改变采购表该采购订单状态 
          $res1 = model('caigou')->where('bianhao',$bianhao)->setField('status',3);
       //获取该采购员
          $caigouyuan = model('caigou')->where('bianhao',$bianhao)->value('caigouyuan');
        //采购获得的积分 
          $jifen = model('caigou')->where('bianhao',$bianhao)->value('jifen');
          //确认收货，采购员积分增加,    token为采购员id,bianhao,leixing为2，zhuangtai为1
           $data = [
               'token'=>$caigouyuan,
               'number'=>$jifen,
               'bianhao'=>$bianhao,
               'leixing'=>2,
               'zhuangtai'=>1,
           ];
           //插入积分记录表
          $res2 = model('jifen')->insertGetId($data);
           //改变用户表的总积分数目
           $caigouyuanInfo = model('user')->where('token',$caigouyuan)->find();
           $res3 = model('user')->where('token',$caigouyuan)->update(['jifen'=>$caigouyuanInfo->jifen+$jifen]);
             if ($res&&$res1&&$res2&&$res3) {
                $json['success']='true';
                $json['msg']='确认收货成功';
                $json['status']= '200';
                $json['jifen'] = $res3;
                $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                echo $m;
             }else{
                $json['success']='false';
                $json['msg']='确认收货失败失败';
                $json['status']=500;
                $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                echo $m;
             }  
      }
       //待支付订单
       public function getDzfOrders($token)
       {
                  //echo $token;
              $data = [
                   'token'=>$token,
                   'zhuangtai'=>1,
                   'status'=>-1,
              ];
            $orders = model('ordermaster')->getDzfOrders($token);
            $time = model('ordermaster')->where($data)->column('create_time');
        
           for ($i=0; $i < count($orders); $i++) { 
                    $orders[$i]['time']=$time[$i];           
            }  
            if ($orders) {
                 $json['success']='true';
                 $json['msg']='待支付订单数据获取成功';
                 $json['status']='200';
                  $json['data']=$orders;
                 $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                 echo $m; 
            }else{
                 $json['success']='false';
                 $json['msg']='待支付订单数据获取失败';
                 $json['status']='500';
                 $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                 echo $m; 
            }
       }

      //待发货订单
        public function getDfhOrders($token)
       {
                  //echo $token;
           $data = [
                   'token'=>$token,
                   'zhuangtai'=>1,
                   'status'=>1,
              ];
            $orders = model('ordermaster')->getDfhOrders($token);
            $time = model('ordermaster')->where($data)->column('create_time');
        
           for ($i=0; $i < count($orders); $i++) { 
                    $orders[$i]['time']=$time[$i];           
            }  
            if ($orders) {
                 $json['success']='true';
                 $json['msg']='待发货订单数据获取成功';
                 $json['status']='200';
                  $json['data']=$orders;
                 $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                 echo $m; 
            }else{
                 $json['success']='false';
                 $json['msg']='待发货订单数据获取失败';
                 $json['status']='500';
                 $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                 echo $m; 
            }
       }
      //已发货
      public function getYfhOrders($token)
       {
                  //echo $token;
           $data = [
                   'token'=>$token,
                   'zhuangtai'=>1,
                   'status'=>0,
              ];
            $orders = model('ordermaster')->getYfhOrders($token);
            $time = model('ordermaster')->where($data)->column('create_time');
        
           for ($i=0; $i < count($orders); $i++) { 
                    $orders[$i]['time']=$time[$i];           
            }  
            if ($orders) {
                 $json['success']='true';
                 $json['msg']='待发货订单数据获取成功';
                 $json['status']='200';
                  $json['data']=$orders;
                 $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                 echo $m; 
            }else{
                 $json['success']='false';
                 $json['msg']='待发货订单数据获取失败';
                 $json['status']='500';
                 $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                 echo $m; 
            }
       }

      //已完成
      public function getYwcOrders($token)
       {
                  //echo $token;
           $data = [
                   'token'=>$token,
                   'zhuangtai'=>1,
                   'status'=>2,
              ];
            $orders = model('ordermaster')->getYwcOrders($token);
            $time = model('ordermaster')->where($data)->column('create_time');
        
           for ($i=0; $i < count($orders); $i++) { 
                    $orders[$i]['time']=$time[$i];           
            }  
            if ($orders) {
                 $json['success']='true';
                 $json['msg']='待发货订单数据获取成功';
                 $json['status']='200';
                  $json['data']=$orders;
                 $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                 echo $m; 
            }else{
                 $json['success']='false';
                 $json['msg']='待发货订单数据获取失败';
                 $json['status']='500';
                 $m = json_encode($json,JSON_UNESCAPED_UNICODE);
                 echo $m; 
            }
       }



}

